//
//  TMAllHomeSubVC.m
//  TMBD Movie App
//
//  Created by zhangkai on 2023/4/2.
//

#import "TMAllHomeSubVC.h"
#import <Masonry.h>
#import "TMHomeSubVCCell.h"
#import "TMTVInfoVC.h"
#import "TMDetailsVc.h"
#import "HomeModel.h"
#import "TMHomeSubBannView.h"
#import "SDCycleScrollView.h"
@interface TMAllHomeSubVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(strong,nonatomic)UICollectionView *mainCollectionView;

@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);

@property(nonatomic,strong)NSArray *listArr;
@end

@implementation TMAllHomeSubVC


- (void)getData{
//
    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"homeall" ofType:@"json"];
    // 将JSON文件转换为NSData
    NSData *data = [NSData dataWithContentsOfFile:jsonPath];
    // 解析JSON数据
    NSError *error = nil;
    id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error) {
        NSLog(@"JSON 解析错误：%@", error.localizedDescription);
    } else {
        NSLog(@"JSON 数据：%@", json);
    }
//
    HomeModel *model = [HomeModel mj_objectWithKeyValues:json];
    self.listArr = @[].mutableCopy;
    NSArray *arr = [model.allDic objectForKey:@"249"];
    if (arr) {
        self.listArr = arr.mutableCopy;
    }
        
        
    
    [self.mainCollectionView reloadData];

    
//
//
//    NSArray *modelArr = [HomeoneModel mj_objectArrayWithKeyValuesArray:model.allDic[@"79"][@"264"]];
//
//
//    NSMutableArray *arr = @[].mutableCopy;
//    NSMutableArray *arr1 = @[].mutableCopy;
//    for (HomeoneModel *tempModel in modelArr) {
//        if([tempModel.title isEqualToString:@"banner"]){
//            [arr addObject:tempModel];
//
//        }else{
//            [arr1 addObject:tempModel];
//        }
//    }
//
//    self.listArr = arr1.copy;
//    self.bannAr = arr.copy;
//
//
//    NSMutableArray *arrpic = @[].mutableCopy;
//    if(arr.count > 0){
//        HomeoneModel *picModel = arr[0];
//        for (NSDictionary *picDic in picModel.allDic) {
//            [arrpic addObject:picDic[@"img"]];
//        }
//    }
//
//    self.mainScro.imageURLStringsGroup = arrpic;
//
//

    
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
   
        [self getData];

    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    self.listArr = @[].copy;
    
    UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:fl];
    [self.view addSubview:self.mainCollectionView];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    [self.mainCollectionView registerClass:[TMHomeSubVCCell class] forCellWithReuseIdentifier:@"TMHomeSubVCCell"];

    
    [self.mainCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.mainCollectionView.backgroundColor = [UIColor colorWithRed:13/255.0 green:19/255.0 blue:35/255.0 alpha:1];

}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return  self.listArr.count;
    

}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    TMHomeSubVCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TMHomeSubVCCell" forIndexPath:indexPath];

        cell.backgroundColor = [UIColor clearColor];
    [cell configUI:self.listArr[indexPath.row]];
    
    

    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TMDetailsVc *VC = [TMDetailsVc new];
    VC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:VC animated:YES];
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;{
    return CGSizeMake((kScreenW-65)/2, 250);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;{
    return  UIEdgeInsetsMake(10, 25, 10, 25);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;{
    return 8;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;{
    return 8;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;{
    
    return CGSizeMake(kScreenW, 0.1);

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;{
    return CGSizeMake(kScreenW, 0.1);
}



#pragma mark - JXPagingViewListViewDelegate

- (UIScrollView *)listScrollView {
    return [UIScrollView new];
}
//
- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}

- (UIView *)listView {
    return self.view;
}

@end
